package pe.edu.upc.jaxws.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

//import pe.edu.upc.jaxws.controller.DespachoController;
import pe.edu.upc.jaxws.model.Despacho;
import java.io.*;
import java.net.*;
import java.util.*;

public class DespachoServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	public DespachoServlet() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		 
		System.out.println("Ingreso serlet");
		
		//URL url = new URL("http://localhost:8080/jaxws-tracking/services/restDespacho/despacho/regDespacho/2805/15-10-2011/15-10-2010/8888/ABC-845/0");
		URL url = new URL("http://localhost:8080/jaxws-tracking/services/restDespacho/despacho/regDespacho/" + 
							request.getParameter("txtCodDespacho") + "/" + 
							request.getParameter("txtFechaRegistro") + "/" + 
							request.getParameter("txtFechaEntrega") + "/" + 
							request.getParameter("txtCantidad") + "/" + 
							request.getParameter("txtPlaca") + "/" + 
							request.getParameter("txtAtencion") );
		
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
		} catch(Throwable t){}
		
		response.sendRedirect("Remito/mensajeOk.jsp");
		
		
		/*HttpClient httpClient = new HttpClient();
	    PostMethod postMethod = new PostMethod("http://localhost:8080/jaxws-tracking/services/restDespacho/despacho/regDespacho/");
	    
	    NameValuePair[] postData = { 
	    	new NameValuePair("codigo", request.getParameter("txtCodDespacho")),
	        new NameValuePair("fechaRegistro", request.getParameter("txtFechaRegistro")),
	        new NameValuePair("fechaEntrega", request.getParameter("txtFechaEntrega")),
	        new NameValuePair("cantidad", request.getParameter("txtCantidad")),
	        new NameValuePair("placa", request.getParameter("txtPlaca")),
	        new NameValuePair("atencion", request.getParameter("txtAtencion"))
	    	};
	    
	    postMethod.addParameters(postData);
	    httpClient.executeMethod(postMethod);
		
	    //por verificar si es que va..
	    response.setContentType("text/html");
	    java.io.PrintWriter out = response.getWriter();
		
	    if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
	        out.println(postMethod.getResponseBodyAsString());
	      } else {
	        out.println("The POST action raised an error: "
	            + postMethod.getStatusLine());
	      }
	    postMethod.releaseConnection();
	    */
		
		System.out.println("Ingreso serlet 2");
		
		/*Gson gson = new Gson();
		String despachoJson = gson.toJson(despacho);
		DespachoController despachoController = new DespachoController();
		despachoController.registraDespacho(despachoJson);*/
		
	}
}
